package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.BaseShortcutMapper;
import com.zjdiepu.www.ibiz.base.IBaseShortcutService;
import com.zjdiepu.www.model.base.dto.BaseShortcut;
import com.zjdiepu.www.model.base.ext.BaseShortcutExt;

/**
 * 快捷方式管理Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年06月27日 17时33分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseShortcutServiceImpl extends BaseService implements IBaseShortcutService {
	public static final Logger logger = LoggerFactory.getLogger(BaseShortcutServiceImpl.class);

	@Autowired
	private BaseShortcutMapper baseShortcutMapper;
	
	@Override
	public void saveUpdateAndDelete(BaseShortcut baseShortcut, OperateType operType) {
		switch (operType) {
		case add:
			baseShortcut.initInsertData(baseShortcut.getModifier(), baseShortcut.getModifierName());
			baseShortcutMapper.insert(baseShortcut);
			break;
		case edit:
			baseShortcut.initUpdateData(baseShortcut.getModifier(), baseShortcut.getModifierName());
			// 影响的记录数
			int updateCount = baseShortcutMapper.updateByPrimaryKeySelective(baseShortcut);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseShortcut.initDeleteData(baseShortcut.getModifier(), baseShortcut.getModifierName());
			// 影响的记录数
			int deleteCount = baseShortcutMapper.updateByPrimaryKeySelective(baseShortcut);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseShortcut> selectByExt(BaseShortcutExt baseShortcutExt) {
		return baseShortcutMapper.selectByExt(baseShortcutExt);
	}
	
	@Override
	public int countByExt(BaseShortcutExt baseShortcutExt){
		return baseShortcutMapper.countByExt(baseShortcutExt);
	}
	
	@Override
	public int countMultiByExt(BaseShortcutExt baseShortcutExt) {
		return baseShortcutMapper.countMultiByExt(baseShortcutExt);
	}
	
	@Override
	public List<BaseShortcut> selectMultiByExt(BaseShortcutExt baseShortcutExt) {
		return baseShortcutMapper.selectMultiByExt(baseShortcutExt);
	}
	
	@Override
	public BaseShortcut selectById(String id) {
		return baseShortcutMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseShortcutMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseShortcut baseShortcut) {
		return baseShortcutMapper.countRefined(baseShortcut);
	}

	@Override
	public BaseShortcut selectRefined(BaseShortcut baseShortcut) {
		return baseShortcutMapper.selectRefined(baseShortcut);
	}
	
	@Override
	public int deletedRefined(BaseShortcut baseShortcut) {
		return baseShortcutMapper.deletedRefined(baseShortcut);
	}

	@Override
	public boolean repeat(BaseShortcut baseShortcut) {
		int count = countRefined(baseShortcut);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseShortcut.getId())) {
			return true;
		}
		BaseShortcut dbItem = selectRefined(baseShortcut);
		if(null == dbItem) {
			return false;
		}
		return !baseShortcut.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseShortcutMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<BaseShortcut> baseShortcuts) {
		return baseShortcutMapper.insertBatch(baseShortcuts);
	}
}
